#pragma GCC optimize(2)
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
//#define pi acos(-1.0)
#define e exp(1.0)
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll maxn=1e3+7;
ll dp[maxn][maxn][2];
ll N;
struct node {
	ll pi,time;
}pos[maxn];
node lef[maxn],rig[maxn];
ll L,R;
int main()
{
//  freopen(".../.txt","w",stdout);
	ios::sync_with_stdio(false);
	cin>>N;
	ll i,j,start;
	for(i=0;i<N;i++){
		cin>>pos[i].pi;
	}
	for(i=0;i<N;i++){
		cin>>pos[i].time;
		if(pos[i].time==0)
		start=i; 
	}
	L=R=0;
	for(i=start-1;i>=0;i--){//处理左边 
		lef[++L].pi=abs(pos[i].pi-pos[start].pi);
		lef[L].time=pos[i].time;
	}
	for(i=start+1;i<N;i++){//处理右边 
		rig[++R].pi=abs(pos[i].pi-pos[start].pi);
		rig[R].time=pos[i].time;
	}
	memset(dp,0x3f,sizeof(dp));
	dp[0][0][0]=dp[0][0][1]=0;
//	dp[1][0][0]=lef[1].pi;
//	dp[0][1][1]=rig[1].pi;
	for(i=0;i<=L;i++){
		for(j=0;j<=R;j++){
			if(i)dp[i][j][0]=min(dp[i-1][j][0]+lef[i].pi-lef[i-1].pi,dp[i-1][j][1]+lef[i].pi+rig[j].pi);
			if(j)dp[i][j][1]=min(dp[i][j-1][1]+rig[j].pi-rig[j-1].pi,dp[i][j-1][0]+lef[i].pi+rig[j].pi);
			if(dp[i][j][0]>lef[i].time&&dp[i][j][1]>rig[j].time){
				cout<<"-1"<<endl;
				return 0;
			}	
			if(dp[i][j][0]>lef[i].time)
			dp[i][j][0]=INF;
			if(dp[i][j][1]>rig[j].time)
			dp[i][j][1]=INF;
		}
	} 
	cout<<min(dp[L][R][0],dp[L][R][1])<<endl;
	return 0;
}
